/*
	用于extjs内置页面
*/
function QueryParams() {}
function Notify() {
    this.dom = {};
    var e = this;
    jsoneditor.util.addEventListener(document, "keydown", function(t) {
        e.onKeyDown(t)
    })
}
function Splitter(e) {
    if (!e || !e.container) throw new Error("params.container undefined in Splitter constructor");
    var t = this;
    jsoneditor.util.addEventListener(e.container, "mousedown", function(e) {
        t.onMouseDown(e)
    }),
    this.container = e.container,
    this.snap = Number(e.snap) || 200,
    this.width = void 0,
    this.value = void 0,
    this.onChange = e.change ? e.change : function() {},
    this.params = {}
}
QueryParams.prototype.getQuery = function() {
    for (var e = window.location.search.substring(1), t = e.split("&"), i = {},
    o = 0, n = t.length; n > o; o++) {
        var s = t[o].split("=");
        if (2 == s.length) {
            var r = decodeURIComponent(s[0]),
            a = decodeURIComponent(s[1]);
            i[r] = a
        }
    }
    return i
},
QueryParams.prototype.setQuery = function(e) {
    var t = "";
    for (var i in e) if (e.hasOwnProperty(i)) {
        var o = e[i];
        void 0 != o && (t.length && (t += "&"), t += encodeURIComponent(i), t += "=", t += encodeURIComponent(e[i]))
    }
    window.location.search = t.length ? "#" + t : ""
},
QueryParams.prototype.getValue = function(e) {
    var t = this.getQuery();
    return t[e]
},
QueryParams.prototype.setValue = function(e, t) {
    var i = this.getQuery();
    i[e] = t,
    this.setQuery(i)
};
var ajax = function() {
    function e(e, t, i, o, n) {
        try {
            var s = new XMLHttpRequest;
            if (s.onreadystatechange = function() {
                4 == s.readyState && n(s.responseText, s.status)
            },
            s.open(e, t, !0), o) for (var r in o) o.hasOwnProperty(r) && s.setRequestHeader(r, o[r]);
            s.send(i)
        } catch(a) {
            n(a, 0)
        }
    }
    function t(t, i, o) {
        e("GET", t, null, i, o)
    }
    function i(t, i, o, n) {
        e("POST", t, i, o, n)
    }
    return {
        fetch: e,
        get: t,
        post: i
    }
} (),
FileRetriever = function(e) {
    e = e || {},
    this.options = {
        maxSize: void 0 != e.maxSize ? e.maxSize : 1048576,
        html5: void 0 != e.html5 ? e.html5 : !0
    },
    this.timeout = Number(e.timeout) || 3e4,
    this.headers = {
        Accept: "application/json"
    },
    this.scriptUrl = e.scriptUrl || "fileretriever.php",
    this.notify = e.notify || void 0,
    this.defaultFilename = "document.json",
    this.dom = {}
};
FileRetriever.prototype._hide = function(e) {
    e.style.visibility = "hidden",
    e.style.position = "absolute",
    e.style.left = "-1000px",
    e.style.top = "-1000px",
    e.style.width = "0",
    e.style.height = "0"
},
FileRetriever.prototype.remove = function() {
    var e = this.dom;
    for (var t in e) if (e.hasOwnProperty(t)) {
        var i = e[t];
        i.parentNode && i.parentNode.removeChild(i)
    }
    this.dom = {}
},
FileRetriever.prototype._getFilename = function(e) {
    return e ? e.replace(/^.*[\\\/]/, "") : ""
},
FileRetriever.prototype.setUrl = function(e) {
    this.url = e
},
FileRetriever.prototype.getFilename = function() {
    return this.defaultFilename
},
FileRetriever.prototype.getUrl = function() {
    return this.url
},
FileRetriever.prototype.loadUrl = function(e, t) {
    this.setUrl(e);
    var i = void 0;
    this.notify && (i = this.notify.showNotification("loading url..."));
    var o = this,
    n = function(e, n) {
        t && (t(e, n), t = void 0),
        o.notify && i && (o.notify.removeMessage(i), i = void 0)
    },
    s = this.scriptUrl;
    ajax.get(e, o.headers, function(t, i) {
        if (200 == i) n(null, t);
        else {
            var r, a = s + "?url=" + encodeURIComponent(e);
            ajax.get(a, o.headers, function(t, i) {
                200 == i ? n(null, t) : 404 == i ? (console.log('Error: url "' + e + '" not found', i, t), r = new Error('Error: url "' + e + '" not found'), n(r, null)) : (console.log('Error: failed to load url "' + e + '"', i, t), r = new Error('Error: failed to load url "' + e + '"'), n(r, null))
            })
        }
    }),
    setTimeout(function() {
        n(new Error("Error loading url (time out)"))
    },
    this.timeout)
},
FileRetriever.prototype.loadFile = function(e) {
    var t = void 0,
    i = this,
    o = function() {
        i.notify && !t && (t = i.notify.showNotification("loading file...")),
        setTimeout(function() {
            n(new Error("Error loading url (time out)"))
        },
        i.timeout)
    },
    n = function(o, n) {
        e && (e(o, n), e = void 0),
        i.notify && t && (i.notify.removeMessage(t), t = void 0)
    },
    s = "fileretriever-upload-" + Math.round(1e15 * Math.random()),
    r = document.createElement("iframe");
    r.name = s,
    i._hide(r),
    r.onload = function() {
        var e = r.contentWindow.document.body.innerHTML;
        if (e) {
            var t = i.scriptUrl + "?id=" + e + "&filename=" + i.getFilename();
            ajax.get(t, i.headers, function(e, t) {
                if (200 == t) n(null, e);
                else {
                    var o = new Error("Error loading file " + i.getFilename());
                    n(o, null)
                }
            })
        }
    },
    document.body.appendChild(r);
    var a = "Microsoft Internet Explorer" == navigator.appName;
    if (a) this.prompt({
        title: "Open file",
        titleSubmit: "Open",
        inputType: "file",
        inputName: "file",
        formAction: this.scriptUrl,
        formMethod: "POST",
        formTarget: s,
        callback: function(e) {
            e && o()
        }
    });
    else {
        var l = document.createElement("form");
        l.action = this.scriptUrl,
        l.method = "POST",
        l.enctype = "multipart/form-data",
        l.target = s,
        this._hide(l);
        var d = document.createElement("input");
        d.type = "file",
        d.name = "file",
        d.onchange = function() {
            o(),
            setTimeout(function() {
                var e = d.value;
                if (e.length) if (i.options.html5 && window.File && window.FileReader) {
                    var t = d.files[0],
                    o = new FileReader;
                    o.onload = function(e) {
                        var t = e.target.result;
                        n(null, t)
                    },
                    o.readAsText(t)
                } else l.submit();
                else n(null, null)
            },
            0)
        },
        l.appendChild(d),
        document.body.appendChild(l),
        setTimeout(function() {
            d.click()
        },
        0)
    }
},
FileRetriever.prototype.loadUrlDialog = function(e) {
    var t = this;
    this.prompt({
        title: "Open url",
        titleSubmit: "Open",
        inputType: "text",
        inputName: "url",
        inputDefault: this.getUrl(),
        callback: function(i) {
            i ? t.loadUrl(i, e) : e()
        }
    })
},
FileRetriever.prototype.prompt = function(e) {
    var t = function() {
        p.parentNode && p.parentNode.removeChild(p),
        n.parentNode && n.parentNode.removeChild(n),
        jsoneditor.util.removeEventListener(document, "keydown", o)
    },
    i = function() {
        t(),
        e.callback && e.callback(null)
    },
    o = jsoneditor.util.addEventListener(document, "keydown", function(e) {
        e = e || window.event;
        var t = e.which || e.keyCode;
        27 == t && (i(), jsoneditor.util.preventDefault(e), jsoneditor.util.stopPropagation(e))
    }),
    n = document.createElement("div");
    n.className = "fileretriever-overlay",
    document.body.appendChild(n);
    var s = document.createElement("form");
    s.className = "fileretriever-form",
    s.target = e.formTarget || "",
    s.action = e.formAction || "",
    s.method = e.formMethod || "POST",
    s.enctype = "multipart/form-data",
    s.encoding = "multipart/form-data",
    s.onsubmit = function() {
        return a.value ? (setTimeout(function() {
            t()
        },
        0), e.callback && e.callback(a.value), void 0 != e.formAction && void 0 != e.formMethod) : (alert("Enter a " + e.inputName + " first..."), !1)
    };
    var r = document.createElement("div");
    r.className = "fileretriever-title",
    r.appendChild(document.createTextNode(e.title || "Dialog")),
    s.appendChild(r);
    var a = document.createElement("input");
    a.className = "fileretriever-field",
    a.type = e.inputType || "text",
    a.name = e.inputName || "text",
    a.value = e.inputDefault || "";
    var l = document.createElement("div");
    l.className = "fileretriever-contents",
    l.appendChild(a),
    s.appendChild(l);
    var d = document.createElement("input");
    d.className = "fileretriever-cancel",
    d.type = "button",
    d.value = e.titleCancel || "Cancel",
    d.onclick = i;
    var h = document.createElement("input");
    h.className = "fileretriever-submit",
    h.type = "submit",
    h.value = e.titleSubmit || "Ok";
    var c = document.createElement("div");
    c.className = "fileretriever-buttons",
    c.appendChild(d),
    c.appendChild(h),
    s.appendChild(c);
    var u = document.createElement("div");
    u.className = "fileretriever-border",
    u.appendChild(s);
    var p = document.createElement("div");
    p.className = "fileretriever-background",
    p.appendChild(u),
    document.body.appendChild(p),
    a.focus(),
    a.select()
},
FileRetriever.prototype.saveFile = function(e, t) {
    var i = void 0;
    this.notify && (i = this.notify.showNotification("saving file..."));
    var o = this,
    n = function(e) {
        t && (t(e), t = void 0),
        o.notify && i && (o.notify.removeMessage(i), i = void 0)
    },
    s = document.createElement("a");
    this.options.html5 && void 0 != s.download ? (s.href = "data:application/json;charset=utf-8," + encodeURIComponent(e), s.download = this.getFilename(), s.click(), n()) : e.length < this.options.maxSize ? ajax.post(o.scriptUrl, e, o.headers, function(e, t) {
        if (200 == t) {
            var i = document.createElement("iframe");
            i.src = o.scriptUrl + "?id=" + e + "&filename=" + o.getFilename(),
            o._hide(i),
            document.body.appendChild(i),
            n()
        } else n(new Error("Error saving file"))
    }) : n(new Error("Maximum allowed file size exceeded (" + this.options.maxSize + " bytes)")),
    setTimeout(function() {
        n(new Error("Error saving file (time out)"))
    },
    this.timeout)
},
Notify.prototype.showNotification = function(e) {
    return this.showMessage({
        type: "notification",
        message: e,
        closeButton: !1
    })
},
Notify.prototype.showError = function(e) {
    return this.showMessage({
        type: "error",
        message: e.message || e.toString(),
        closeButton: !0
    })
},
Notify.prototype.showMessage = function(e) {
    var t = this.dom.frame;
    if (!t) {
        var i = 500,
        o = 5,
        n = document.body.offsetWidth || window.innerWidth;
        t = document.createElement("div"),
        t.style.position = "absolute",
        t.style.left = (n - i) / 2 + "px",
        t.style.width = i + "px",
        t.style.top = o + "px",
        t.style.zIndex = "999",
        document.body.appendChild(t),
        this.dom.frame = t
    }
    var s = e.type || "notification",
    r = e.closeButton !== !1,
    a = document.createElement("div");
    a.className = s,
    a.type = s,
    a.closeable = r,
    a.style.position = "relative",
    t.appendChild(a);
    var l = document.createElement("table");
    l.style.width = "100%",
    a.appendChild(l);
    var d = document.createElement("tbody");
    l.appendChild(d);
    var h = document.createElement("tr");
    d.appendChild(h);
    var c = document.createElement("td");
    if (c.innerHTML = e.message || "", h.appendChild(c), r) {
        var u = document.createElement("td");
        u.style.textAlign = "right",
        u.style.verticalAlign = "top",
        h.appendChild(u);
        var p = document.createElement("button");
        p.innerHTML = "&times;",
        p.title = "Close message (ESC)",
        u.appendChild(p);
        var m = this;
        p.onclick = function() {
            m.removeMessage(a)
        }
    }
    return a
},
Notify.prototype.removeMessage = function(e) {
    var t = this.dom.frame;
    if (!e && t) {
        for (var i = t.firstChild; i && !i.closeable;) i = i.nextSibling;
        i && i.closeable && (e = i)
    }
    e && e.parentNode == t && e.parentNode.removeChild(e),
    t && 0 == t.childNodes.length && (t.parentNode.removeChild(t), delete this.dom.frame)
},
Notify.prototype.onKeyDown = function(e) {
    e = e || window.event;
    var t = e.which || e.keyCode;
    27 == t && (this.removeMessage(), jsoneditor.util.preventDefault(e), jsoneditor.util.stopPropagation(e))
},
Splitter.prototype.onMouseDown = function(e) {
    var t = this,
    i = e.which ? 1 == e.which : 1 == e.button;
    i && (jsoneditor.util.addClassName(this.container, "active"), this.params.mousedown || (this.params.mousedown = !0, this.params.mousemove = jsoneditor.util.addEventListener(document, "mousemove", function(e) {
        t.onMouseMove(e)
    }), this.params.mouseup = jsoneditor.util.addEventListener(document, "mouseup", function(e) {
        t.onMouseUp(e)
    }), this.params.screenX = e.screenX, this.params.changed = !1, this.params.value = this.getValue()), jsoneditor.util.preventDefault(e), jsoneditor.util.stopPropagation(e))
},
Splitter.prototype.onMouseMove = function(e) {
    if (void 0 != this.width) {
        var t = e.screenX - this.params.screenX,
        i = this.params.value + t / this.width;
        i = this.setValue(i),
        i != this.params.value && (this.params.changed = !0),
        this.onChange(i)
    }
    jsoneditor.util.preventDefault(e),
    jsoneditor.util.stopPropagation(e)
},
Splitter.prototype.onMouseUp = function(e) {
    if (jsoneditor.util.removeClassName(this.container, "active"), this.params.mousedown) {
        jsoneditor.util.removeEventListener(document, "mousemove", this.params.mousemove),
        jsoneditor.util.removeEventListener(document, "mouseup", this.params.mouseup),
        this.params.mousemove = void 0,
        this.params.mouseup = void 0,
        this.params.mousedown = !1;
        var t = this.getValue();
        this.params.changed || (0 == t && (t = this.setValue(.2), this.onChange(t)), 1 == t && (t = this.setValue(.8), this.onChange(t)))
    }
    jsoneditor.util.preventDefault(e),
    jsoneditor.util.stopPropagation(e)
},
Splitter.prototype.setWidth = function(e) {
    this.width = e
},
Splitter.prototype.setValue = function(e) {
    e = Number(e),
    void 0 != this.width && this.width > this.snap && (e < this.snap / this.width && (e = 0), e > (this.width - this.snap) / this.width && (e = 1)),
    this.value = e;
    try {
        localStorage.splitterValue = e
    } catch(t) {
        console.log(t)
    }
    return e
},
Splitter.prototype.getValue = function() {
    var e = this.value;
    if (void 0 == e) try {
        void 0 != localStorage.splitterValue && (e = Number(localStorage.splitterValue), e = this.setValue(e))
    } catch(t) {
        console.log(t)
    }
    return void 0 == e && (e = this.setValue(.5)),
    e
};
var treeEditor = null,
codeEditor = null,
JsonApp = {};
JsonApp.content = null;
JsonApp.superViewKey = null;
JsonApp.codeEditorKey = 'codeEditor';
JsonApp.treeEditorKey = 'treeEditor';
JsonApp.CodeToTree = function() {
    try {
        treeEditor.set(codeEditor.get())
    } catch(e) {
        JsonApp.notify.showError(JsonApp.formatError(e))
    }
},
JsonApp.treeToCode = function() {
    try {
        codeEditor.set(treeEditor.get())
    } catch(e) {
        JsonApp.notify.showError(JsonApp.formatError(e))
    }
},
JsonApp.isJson = function(obj){
	var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;    
	return isjson;
},
JsonApp.load = function(cEdit, tEdit) {
    try {
    	
    	if (cEdit) {
	    	JsonApp.codeEditorKey = cEdit;
    	}
    	if (tEdit) {
	    	JsonApp.treeEditorKey = tEdit;
    	}
		
        JsonApp.notify = new Notify,
        JsonApp.retriever = new FileRetriever({
            scriptUrl: "fileretriever.php",
            notify: JsonApp.notify
        });
        var e;
        if (window.QueryParams) {
            var t = new QueryParams,
            i = t.getValue("url");
            i && (e = JsonApp.content,
            JsonApp.openUrl(i))
        }
        JsonApp.lastChanged = void 0;
        var o = document.getElementById(JsonApp.codeEditorKey);
        codeEditor = new jsoneditor.JSONEditor(o, {
            mode: "code",
            change: function() {
                JsonApp.lastChanged = codeEditor
            },
            error: function(e) {
                JsonApp.notify.showError(JsonApp.formatError(e))
            }
        });
        e = JsonApp.content?JsonApp.content:e;
        codeEditor.set(e);
        o = document.getElementById(JsonApp.treeEditorKey);
        treeEditor = new jsoneditor.JSONEditor(o, {
            mode: "tree",
            change: function() {
                JsonApp.lastChanged = treeEditor
            },
            error: function(e) {
                JsonApp.notify.showError(JsonApp.formatError(e))
            }
        });
        if ( JsonApp.isJson(JsonApp.content) ) {
	        e = JsonApp.content;
        }
        if ( JsonApp.isJson(e) ) {
	        treeEditor.set(e);
        } else {
	        var ee;
	        treeEditor.set(ee);
        }
        JsonApp.splitter = new Splitter({
            container: document.getElementById("drag"),
            change: function() {
                JsonApp.resize();
            }
        });
        var n = document.getElementById("toTree");
        n.onclick = function() {
            this.focus(),
            JsonApp.CodeToTree()
        };
        var s = document.getElementById("toCode");
        s.onclick = function() {
            this.focus(),
            JsonApp.treeToCode()
        },
        jsoneditor.util.addEventListener(window, "resize", JsonApp.resize);
        return;
        var r = document.getElementById("clear");
        r.onclick = JsonApp.clearFile;
        var a = document.getElementById("menuOpenFile");
        a.onclick = function(e) {
            JsonApp.openFile(),
            jsoneditor.util.stopPropagation(e),
            jsoneditor.util.preventDefault(e)
        };
        var l = document.getElementById("menuOpenUrl");
        l.onclick = function(e) {
            JsonApp.openUrl(),
            jsoneditor.util.stopPropagation(e),
            jsoneditor.util.preventDefault(e)
        };
        var d = document.getElementById("save");
        d.onclick = JsonApp.saveFile,
        codeEditor.focus(),
        document.body.spellcheck = !1
    } catch(h) {
        JsonApp.notify.showError(h)
    }
},
JsonApp.openCallback = function(e, t) {
    if (e) JsonApp.notify.showError(e);
    else if (null != t) {
        codeEditor.setText(t);
        try {
            var i = jsoneditor.util.parse(t);
            treeEditor.set(i)
        } catch(e) {
            treeEditor.set({}),
            JsonApp.notify.showError(JsonApp.formatError(e))
        }
    }
},
JsonApp.openFile = function() {
    JsonApp.retriever.loadFile(JsonApp.openCallback)
},
JsonApp.openUrl = function(e) {
    e ? JsonApp.retriever.loadUrl(e, JsonApp.openCallback) : JsonApp.retriever.loadUrlDialog(JsonApp.openCallback)
},
JsonApp.saveFile = function() {
    JsonApp.lastChanged == treeEditor && JsonApp.treeToCode(),
    JsonApp.lastChanged = void 0;
    var e = codeEditor.getText();
    JsonApp.retriever.saveFile(e, function(e) {
        e && JsonApp.notify.showError(e)
    })
},
JsonApp.formatError = function(e) {
    var t = '<pre class="error">' + e.toString() + "</pre>";
    return "undefined" != typeof jsonlint && (t += '<a class="error" href="http://zaach.github.com/jsonlint/" target="_blank">validated by jsonlint</a>'),
    t
},
JsonApp.clearFile = function() {
    var e;
    codeEditor.set(e),
    treeEditor.set(e)
},
JsonApp.resize = function() {
    var t = document.getElementById("treeEditor"),
    i = document.getElementById("codeEditor"),
    o = document.getElementById("splitter"),
    n = document.getElementById("buttons"),
    s = document.getElementById("drag"),
    r = document.getElementById("ad"),
    a = 15,
    l = window.innerWidth || document.body.offsetWidth || document.documentElement.offsetWidth,
    d = r ? r.clientWidth : 0;
    if (JsonApp.superViewKey==null) {
		if (d && (l -= d + a), JsonApp.splitter) {
	        JsonApp.splitter.setWidth(l);
	        var h = JsonApp.splitter.getValue(),
	        c = h > 0,
	        u = 1 > h,
	        p = c && u;
	        n.style.display = p ? "" : "none";
	        var m, f = o.clientWidth;
	        if (c) if (u) {
	            m = l * h - f / 2;
	            var v = 8 == jsoneditor.util.getInternetExplorerVersion();
	            s.innerHTML = v ? "|" : "&#8942;",
	            s.title = "向左或向右拖动调整面板大小"
	        } else m = l * h - f,
	        s.innerHTML = "&lsaquo;",
	        s.title = "向左拖动显示 Tree Editor";
	        else m = 0,
	        s.innerHTML = "&rsaquo;",
	        s.title = "向右拖动显示 Code Editor";
	        i.style.display = 0 == h ? "none" : "",
	        i.style.width = Math.max(Math.round(m), 0) + "px",
	        codeEditor.resize(),
	        s.style.height = o.clientHeight - n.clientHeight - 2 * a - (p ? a : 0) + "px",
	        s.style.lineHeight = s.style.height,
	        t.style.display = 1 == h ? "none" : "",
	        t.style.left = Math.round(m + f) + "px",
	        t.style.width = Math.max(Math.round(l - m - f - 2), 0) + "px"
	    }
	    
	    t.style.padding = '0 0 0 0';
        t.style.margin = '0 0 0 0 ';
        i.style.padding = '0 0 0 0';
        i.style.margin = '0 0 0 0';
    }
};